/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.dakaqi.activity.services;

import cn.dakaqi.entities.Activity;
import cn.dakaqi.entities.Group;
import cn.dakaqi.entities.Volunteer;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

public interface ActivityDao extends PagingAndSortingRepository<Activity, Long>, JpaSpecificationExecutor<Activity>
{

    Activity findByActivityCode(String activityCode) throws ServiceRuntimeException;

    Page<Activity> findByName(String name, Pageable pageRequest);

    Page<Activity> findByGroup(Group group, Pageable pageRequest);

    Page<Activity> findByCreateUser(Volunteer volunteer, Pageable pageRequest);


    //	@Modifying
//	@Query("delete from Activity activity where activity.user.id=?1")
//	void deleteByUserId(Long id);

    @Query(nativeQuery = true, value = "select CODE as act from U_AREA_PROVINCE where NAME like %:province%")
    public String queryProvinceCode(@Param("province") String province);

    @Modifying
    @Query(nativeQuery = true,value = "insert into U_ACTIVITY_APPLY(CREATE_TIME,ROLE,STATUS,ACTIVITY_ID,VOLUNTEER_ID) VALUES(:createTime,:role,:status,:actId,:vId)")
    public void applyActivity(@Param("createTime") Date createTime,@Param("role") int role,@Param("status") int status,@Param("actId") Long actId,@Param("vId") Long vId);

    @Query(nativeQuery = true, value = "select count(*) as act from U_ACTIVITY where DEL_STATUS = 0 and STATUS=:STATUS")
    public int countByStatus(@Param("STATUS") int status);

    List<Activity> findByNameLike(String content);

    @Query(nativeQuery = true, value = "select count(*) as act from U_ACTIVITY where GROUP_ID=:GROUP_ID and DEL_STATUS = 0 and STATUS=:STATUS")
    int countByGroupAndStatus(@Param("GROUP_ID") long GROUP_ID, @Param("STATUS") int status);
}
